package work.chief.backend.core.builder;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import work.chief.backend.core.builder.sql.MySQLDialect;
import work.chief.backend.core.builder.sql.SQLDialect;
import work.chief.backend.core.generator.DataGenerator;
import work.chief.backend.core.generator.DataGeneratorFactory;
import work.chief.backend.domain.Field;
import work.chief.backend.domain.enumeration.MockTypeEnum;
import work.chief.backend.domain.schame.TableSchema;

import java.util.*;


/**
 * 数据生成
 */
public class DataBuilder {

    SQLDialect sqlDialect=new MySQLDialect();

    public static List<Map<String, Object>> generateData(TableSchema tableSchema, int rowCount) {
        List<Field> fieldList = tableSchema.getFieldList();

//        初始化结果集合
        ArrayList<Map<String, Object>> dataList = new ArrayList<>(rowCount);
        for (int i = 0; i < rowCount; i++) {
            dataList.add(new HashMap<>());
        }
        for (Field field : fieldList) {
//            根据字段信息获取模拟类型枚举
            MockTypeEnum mockTypeEnum = Optional.ofNullable(MockTypeEnum.getEnumByValue(field.getMockType())).orElse(MockTypeEnum.NONE);
//            使用生成工厂，根据模拟类型获取生成器
            DataGenerator generator = DataGeneratorFactory.getGenerator(mockTypeEnum);
            List<String> mockDataList = generator.doGenerate(field, rowCount);
            String fieldName = field.getFieldName();
            if (CollectionUtils.isNotEmpty(mockDataList)) {
                for (int i = 0; i < rowCount; i++) {
                    dataList.get(i).put(fieldName, mockDataList.get(i));
                }
            }
        }
        return dataList;
    }

}
